const api = require('../../utils/api.js');

Component({
    properties: {
        title: String,
        hasBackground: { //是否有背景图案
            type: Boolean,
            value: false
        },
        showGoBack: { //是否显示返回按钮
            type: Boolean,
            value: true
        },
        showGoHome: {
            type: Boolean,
            value: false
        },
        showScan: { //是否显示扫码按钮
            type: Boolean,
            value: false
        },
        customGoBack: {
            type: Boolean,
            value: false
        },
    },
    data: {
        navBarHeight: 0, //顶部导航高度
    },
    lifetimes: {
        attached() {
            const systemInfo = wx.getWindowInfo();
            this.setData({
                navBarHeight: systemInfo.statusBarHeight + 44,
            });
        },
    },
    methods: {
        goBack() {
            if(this.data.customGoBack) {
                this.triggerEvent('goBack');
            } else {
                wx.navigateBack(-1);
            }
        },
        goHome() {
            wx.switchTab({
                url: '/pages/index/index',
            })
        },

        scanQRcode() { //扫码
            wx.scanCode({
                // onlyFromCamera: true, // 只允许从相机扫码
                scanType: ['qrCode'],
            })
            .then(data => {
                if (data.result && data.result.indexOf('{') > -1) {
                    let result = JSON.parse(data.result)
                    console.log(result)
                    if (result.k == 'ORDER_WRITEOFF') { //签到二维码
                        this.submitWriteoff(result.v.id);
                    }
                } else {
                    wx.showToast({
                        title: '请扫描有效的二维码',
                        icon: 'none'
                    })
                }
            })
        },
        submitWriteoff(id) { //提交扫码结果
            api.post('/order/pickup?id=' + id)
                .then(() => {
                    wx.showToast({
                        title: '核销成功！',
                        icon: 'success'
                    });
                })
        },
    }
})